package com.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.mapper.PomrDao;
import com.domain.Bls;
import com.domain.Cj;
import com.domain.Cs;
import com.domain.Hyzk;
import com.domain.Jb;
import com.domain.Jzlx;
import com.domain.Pomr;
import com.domain.Rllx;
import com.domain.Rqlx;
import com.domain.Ss;
import com.domain.Sx;
import com.domain.UserBls;
import com.domain.UserCj;
import com.domain.UserCs;
import com.domain.UserRllx;
import com.domain.UserYs;
import com.domain.UserYwgms;
import com.domain.Whcd;
import com.domain.Ws;
import com.domain.Xx;
import com.domain.Ys;
import com.domain.Ywgms;
import com.domain.Zjlx;
import com.domain.Zw;
import com.service.PomrService;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Service(value="pomrService")
public class PomrServiceImpl implements PomrService {

	@Autowired
	PomrDao pomrDao;
	
	@Override
	public List<Pomr> getPomr(Map params) {
		List<Pomr> pomrs = null;
		pomrs = pomrDao.getPomrs(params);
		return pomrs;
	}

	@Override
	public int getCount(Map params) {
		int count = 0;
		count = pomrDao.getCount(params);
		return count;
	}

	@Override
	public Pomr getPomrByUid(int uid) {
		Pomr pomr = pomrDao.getPomr(uid);
		return pomr;
	}
	
	@Override
	public Pomr getPomrByJkdah(int jkdah) {
		Pomr pomr = pomrDao.getPomr(jkdah);
		return pomr;
	}

	@Override
	public Map<String,Object> getPomrMapInfo(int uid) {
		Map<String,Object> map = pomrDao.getPomrInfo(uid);
		return map;
	}

	/**
	 * 获得一些一对多的信息
	 */
	@Override
	public Map<String,List<Object>> getPomraCascade(int uid) {
		Map<String,List<Object>> mapPomrCascade = new HashMap<String,List<Object>>();
		List<Map<String,Object>> listYwgms = pomrDao.getYwgms(uid);
		List<Map<String,Object>> listBls = pomrDao.getBls(uid);
		List<Map<String,Object>> listCj = pomrDao.getCj(uid);
		List<Map<String,Object>> listRllx = pomrDao.getRllx(uid);
		List<Map<String,Object>> listYs = pomrDao.getYs(uid);
		List<Map<String,Object>> listCs = pomrDao.getCs(uid);
		
		//药物过敏史
		List<Object> tmpYwgms = new ArrayList<Object>();
		for(int i=0;i<listYwgms.size();i++){
			tmpYwgms.add(listYwgms.get(i).get("ywgms"));
		}
		mapPomrCascade.put("ywgms",tmpYwgms);
		
		//暴露史
		List<Object> tmpBls = new ArrayList<Object>();
		for(int i=0;i<listBls.size();i++){
			tmpBls.add(listBls.get(i).get("blmc"));
		}
		mapPomrCascade.put("bls",tmpBls);
		
		//残疾
		List<Object> tmpCj = new ArrayList<Object>();
		for(int i=0;i<listCj.size();i++){
			tmpCj.add(listCj.get(i).get("mc"));
		}
		mapPomrCascade.put("cj",tmpCj);
		
		//燃料类型
		List<Object> tmpRllx = new ArrayList<Object>();
		for(int i=0;i<listRllx.size();i++){
			tmpRllx.add(listRllx.get(i).get("mc"));
		}
		mapPomrCascade.put("rllx",tmpRllx);
		
		//饮水
		List<Object> tmpYs = new ArrayList<Object>();
		for(int i=0;i<listYs.size();i++){
			tmpYs.add(listYs.get(i).get("mc"));
		}
		mapPomrCascade.put("ys",tmpYs);

		//厕所
		List<Object> tmpCs = new ArrayList<Object>();
		for(int i=0;i<listCs.size();i++){
			tmpCs.add(listCs.get(i).get("mc"));
		}
		mapPomrCascade.put("cs",tmpCs);		
		
		return mapPomrCascade;
	}
	
	
	@Override
	public Map<String, List<Map<String, Object>>> getPomrCascadeSJ(int uid) {
		Map<String, List<Map<String, Object>>> mapPomrCascadeSJ  = new HashMap<String, List<Map<String, Object>>>();
		
		//疾病
		mapPomrCascadeSJ.put("jb", pomrDao.getJb(uid));
		
		//手术
		mapPomrCascadeSJ.put("ss", pomrDao.getSs(uid));
	
		//外伤
		mapPomrCascadeSJ.put("ws", pomrDao.getWs(uid));

		//输血
		mapPomrCascadeSJ.put("sx", pomrDao.getSx(uid));
		
		return mapPomrCascadeSJ;
	}

	@Override
	public Map<String, List<Object>> getDm() {
		Map<String,List<Object>> mapDm = new HashMap<String,List<Object>>(); 
		
		//药物过敏史
		List<Ywgms> ListYwgms = pomrDao.getDmYwgms();
		List<Object> tmpYwgms = new ArrayList<Object>();
		for(int i=0;i<ListYwgms.size();i++){
			tmpYwgms.add(ListYwgms.get(i).getMc());
		}
		mapDm.put("ywgms", tmpYwgms);
		
		//暴露史
		List<Bls> ListBls = pomrDao.getDmBls();
		List<Object> tmpBls = new ArrayList<Object>();
		for(int i=0;i<ListBls.size();i++){
			tmpBls.add(ListBls.get(i).getMc());
		}
		mapDm.put("bls", tmpBls);
		
		//残疾
		List<Cj> ListCj = pomrDao.getDmCj();
		List<Object> tmpCj = new ArrayList<Object>();
		for(int i=0;i<ListCj.size();i++){
			tmpCj.add(ListCj.get(i).getMc());
		}
		mapDm.put("cj", tmpCj);
		
		//燃料类型
		List<Rllx> ListRllx = pomrDao.getDmRllx(); 
		List<Object> tmpRllx = new ArrayList<Object>();
		for(int i=0;i<ListRllx.size();i++){
			tmpRllx.add(ListRllx.get(i).getMc());
		}
		mapDm.put("rllx", tmpRllx);
	
		//饮水
		List<Ys> ListYs = pomrDao.getDmYs(); 
		List<Object> tmpYs = new ArrayList<Object>();
		for(int i=0;i<ListYs.size();i++){
			tmpYs.add(ListYs.get(i).getMc());
		}
		mapDm.put("ys", tmpYs);		
		
		//厕所
		List<Cs> ListCs = pomrDao.getDmCs(); 
		List<Object> tmpCs = new ArrayList<Object>();
		for(int i=0;i<ListCs.size();i++){
			tmpCs.add(ListCs.get(i).getMc());
		}
		mapDm.put("cs", tmpCs);
		
		//疾病
		List<Jb> ListJb = pomrDao.getDmJb();
		List<Object> tmpJb = new ArrayList<Object>();
		for(int i=0;i<ListJb.size();i++){
			tmpJb.add(ListJb.get(i).getMc());
		}
		mapDm.put("jb", tmpJb);
		
		//手术
		List<Ss> ListSs = pomrDao.getDmSs();
		List<Object> tmpSs = new ArrayList<Object>();
		for(int i=0;i<ListSs.size();i++){
			tmpSs.add(ListSs.get(i).getMc());
		}
		mapDm.put("ss", tmpSs);
		
		//外伤
		List<Ss> ListWs = pomrDao.getDmSs();
		List<Object> tmpWs = new ArrayList<Object>();
		for(int i=0;i<ListWs.size();i++){
			tmpWs.add(ListWs.get(i).getMc());
		}
		mapDm.put("ws", tmpWs);
		
		//输血
		List<Sx> ListSx = pomrDao.getDmSx();
		List<Object> tmpSx = new ArrayList<Object>();
		for(int i=0;i<ListSx.size();i++){
			tmpSx.add(ListSx.get(i).getMc());
		}
		mapDm.put("sx", tmpSx);		
		
		return mapDm;
	}

	@Override
	public List<Zjlx> getDmZjlx() {
		List<Zjlx> listZjlx = pomrDao.getZjlx();
		return listZjlx;
	}

	@Override
	public List<Hyzk> getDmHyzk() {
		return pomrDao.getHyzk();
	}

	@Override
	public List<Zw> getDmZw() {
		return pomrDao.getZw();
	}

	@Override
	public List<Whcd> getDmWhcd() {
		// TODO Auto-generated method stub
		return pomrDao.getWhcd();
	}

	@Override
	public List<Xx> getDmXx() {
		// TODO Auto-generated method stub
		return pomrDao.getXx();
	}

	@Override
	public List<Rqlx> getDmRqlx() {
		// TODO Auto-generated method stub
		return pomrDao.getRqlx();
	}

	@Override
	public List<Jzlx> getDmJzlx() {
		// TODO Auto-generated method stub
		return pomrDao.getJzlx();
	}
	
	@Override
	public List<Ywgms> getDmYwgms() {
		// TODO Auto-generated method stub
		return pomrDao.getDmYwgms();
	}
	
	@Override
	public List<Bls> getDmBls() {
		// TODO Auto-generated method stub
		return pomrDao.getDmBls();
	}
	
	@Override
	public List<Cj> getDmCj() {
		// TODO Auto-generated method stub
		return pomrDao.getDmCj();
	}
	
	@Override
	public List<Rllx> getDmRllx() {
		// TODO Auto-generated method stub
		List<Rllx> rllxlist = pomrDao.getDmRllx();
		return rllxlist;
	}
	
	@Override
	public List<Cs> getDmCs() {
		// TODO Auto-generated method stub
		return pomrDao.getDmCs();
	}
	
	@Override
	public List<Ys> getDmYs() {
		// TODO Auto-generated method stub
		return pomrDao.getDmYs();
	}
	
	@Override
	public List<Jb> getDmJb() {
		// TODO Auto-generated method stub
		return pomrDao.getDmJb();
	}
	
	@Override
	public List<Ss> getDmSs() {
		// TODO Auto-generated method stub
		return pomrDao.getDmSs();
	}

	@Override
	public List<Ws> getDmWs() {
		// TODO Auto-generated method stub
		return pomrDao.getDmWs();
	}

	@Override
	public List<Sx> getDmSx() {
		// TODO Auto-generated method stub
		return pomrDao.getDmSx();
	}
	
	
	@Override
	public void updatePomr(Pomr pomr) {
		pomrDao.updatePomr(pomr);
		
	}
	
	@Override
	public void addPomr(Pomr pomr) {
		pomrDao.addPorm(pomr);
		
	}	
	

	@Override
	public int getYwgmsIdByMc(String mc) {
		return pomrDao.getYwgmsIdByMc(mc);
	}

	@Override
	public void deleteUserYwgms(int uid) {
		pomrDao.deleteUserYwgms(uid);
	}

	@Override
	public void addUserYwgms(UserYwgms userywgms) {
		pomrDao.addUserYwgms(userywgms);
	}

	@Override
	public int getBlsIdByMc(String mc) {
		return pomrDao.getBlsIdByMc(mc);
	}

	@Override
	public void deleteUserBls(int uid) {
		pomrDao.deleteUserBls(uid);
	}

	@Override
	public void addUserBls(UserBls userbls) {
		pomrDao.addUserBls(userbls);
	}

	@Override
	public int getCjIdByMc(String mc) {
		return pomrDao.getCjIdByMc(mc);
	}

	@Override
	public void deleteUserCj(int uid) {
		pomrDao.deleteUserCj(uid);
	}

	@Override
	public void addUserCj(UserCj usercj) {
		pomrDao.addUserCj(usercj);
	}

	@Override
	public int getRllxIdByMc(String mc) {
		return pomrDao.getRllxIdByMc(mc);
	}

	@Override
	public void deleteUserRllx(int uid) {
		pomrDao.deleteUserRllx(uid);
	}

	@Override
	public void addUserRllx(UserRllx userrllx) {
		pomrDao.addUserRllx(userrllx);
	}

	@Override
	public int getYsIdByMc(String mc) {
		return pomrDao.getYsIdByMc(mc);
	}

	@Override
	public void deleteUserYs(int uid) {
		pomrDao.deleteUserYs(uid);
	}

	@Override
	public void addUserYs(UserYs userys) {
		pomrDao.addUserYs(userys);
		
	}

	@Override
	public int getCsIdByMc(String mc) {
		return pomrDao.getCsIdByMc(mc);
	}

	@Override
	public void deleteUserCs(int uid) {
		pomrDao.deleteUserCs(uid);
	}

	@Override
	public void addUserCs(UserCs usercs) {
		pomrDao.addUserCs(usercs);
	}

	@Override
	public int getUidByJkdah(int jkdah) {
		
		return pomrDao.getUidByJkdah(jkdah);
	}

	@Override
	public Map<String,Object> getPomrByUid2(int uid) {
		return pomrDao.getPomr2(uid);
	}

	@Override
	public Ret doChangePwd(int ucode, String oldpwd,String newpwd,String newpwd2) throws Exception {
		
		Ret ret = new Ret();
		
		int count = pomrDao.getOldPwd(ucode, oldpwd);
		if(count==0){
			ret.setStatusCode("300");
			ret.setMessage("密码修改失败，输入的旧密码不正确！");
		}else{
			if(newpwd2.equals(newpwd)){
				pomrDao.updatePwdByUcode(ucode,newpwd);
				ret.setStatusCode("200");
				ret.setMessage("密码修改成功！");
			}else{
				ret.setStatusCode("300");
				ret.setMessage("密码修改失败，两次输入的密码不一致！");
			}
		}
		
		return ret;
	}

	@Override
	public int getNewJkdah(int ocode) {
		
		Map<String,Object> mapNum = pomrDao.getLastNum();
		
		int maxNum = ObjectUtils.getIntVal(mapNum.get("num").toString());
		
		return ocode*100000+maxNum +3;
	}

	@Override
	public int getUidByUcode(int ucode) {
		Map<String,Object> map = pomrDao.getUidByUcode(ucode);
		
		int uid = ObjectUtils.getIntVal(map.get("uid").toString());
		
		return uid;
		
	}

	

	
}
